﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using log4net;

namespace Riel_Gestion
{
    public partial class FrmPropiedades : Form
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(FrmPropiedades));
        private static String ID_FECHA_PRED = "fechaPred";
        private static String ID_PER_BACK = "perBackAuto";
        private static String ID_WALLPAPPER = "wallpapper";
        private static String ID_WPLAYOUT = "wpLayout";

        private String wallpapper;

        public String Wallpapper
        {
            get { return wallpapper; }
            set { wallpapper = value; }
        }

        private String wpLayout = "Expandido";

        public String WpLayout
        {
            get { return wpLayout; }
            set { wpLayout = value; }
        }


        private int fechaPred = 1;

        public int FechaPred
        {
            get { return fechaPred; }
            set { fechaPred = value; }
        }
        private int perBack = 1;

        public int PerBack
        {
            get { return perBack; }
            set { perBack = value; }
        }

        public FrmPropiedades()
        {
            InitializeComponent();     
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName;
            if (Environment.OSVersion.Version.Major >= 6)
                path = Directory.GetParent(path).ToString();
            
            ofd.InitialDirectory = path;
            ofd.FileName = "";
            ofd.Filter = "Archivos de imagenes (*.bmp;*.jpg)|*.bmp;*.jpg|All files (*.*)|*.*";            
            
            DialogResult dResult = ofd.ShowDialog();

            if (dResult == DialogResult.OK)
            {
                wallpapper = ofd.FileName;
                pictureBox.Image = Image.FromFile(ofd.FileName);
                pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox.Refresh();

                LocalProperties.getInstance().setProperty("wallpaper",ofd.FileName);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmPropiedades_Load(object sender, EventArgs e)
        {
            //Cargo la imagen
            try
            {
                wallpapper = LocalProperties.getInstance().getProperty(ID_WALLPAPPER);
                if (!wallpapper.Equals(""))
                {
                    pictureBox.Image = Image.FromFile(LocalProperties.getInstance().getProperty(ID_WALLPAPPER));
                    pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
                }
                else
                {
                    chkNoWp.CheckState = CheckState.Checked;
                }
            }
            catch (Exception ex)
            {
                logger.Warn(ex.Message);
            }

            //Cargo el Layout de la imagen
            wpLayout = LocalProperties.getInstance().getProperty(ID_WPLAYOUT);

            if (rBCent.Text.Equals(wpLayout))
                rBCent.Select();
            if (rBExp.Text.Equals(wpLayout))
                rBExp.Select();
            if (rBMos.Text.Equals(wpLayout))
                rBMos.Select();
            
            //Cargo la fecha por defecto para filtros
            fechaPred = int.Parse(LocalProperties.getInstance().getProperty(ID_FECHA_PRED));
            switch (fechaPred)
            {
                case 1:
                    rB1.Select();
                    break;
                case 2:
                    rB2.Select();
                    break;
                case 3:
                    rB3.Select();
                    break;
                case 4:
                    rB4.Select();
                    break;
                case 5:
                    rB5.Select();
                    break;
                case 6:
                    rB6.Select();
                    break;
            }

            //Cargo Periodo Backups
            perBack = int.Parse(LocalProperties.getInstance().getProperty(ID_PER_BACK));
            switch (perBack)
            {
                case 2:
                    rB2Horas.Select();
                    break;
                case 4:
                    rB4Horas.Select();
                    break;
                case 6:
                    rB6Horas.Select();
                    break;                
            }
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            //Updatear los campos            
            //Update imagen
            if(chkNoWp.Checked)
                LocalProperties.getInstance().setProperty(ID_WALLPAPPER, "");
            else
                LocalProperties.getInstance().setProperty(ID_WALLPAPPER, wallpapper);

            //Update layout
            if (rBCent.Checked)
                wpLayout = rBCent.Text;
            if (rBExp.Checked)
                wpLayout = rBExp.Text;
            if (rBMos.Checked)
                wpLayout = rBMos.Text;

            LocalProperties.getInstance().setProperty(ID_WPLAYOUT, wpLayout);


            //Fecha por defecto
            int valorFecha = 0;

            if (rB1.Checked)
                valorFecha = 1;
            if (rB2.Checked)
                valorFecha = 2;
            if (rB3.Checked)
                valorFecha = 3;
            if (rB4.Checked)
                valorFecha = 4;
            if (rB5.Checked)
                valorFecha = 5;
            if (rB6.Checked)
                valorFecha = 6;
                        
            LocalProperties.getInstance().setProperty(ID_FECHA_PRED,valorFecha.ToString());
            
            //periodo backup por defecto
            int perBak = 0;

            if (rB2Horas.Checked)
                perBak = 2;
            if (rB4Horas.Checked)
                perBak = 4;
            if (rB6Horas.Checked)
                perBak = 6;

            LocalProperties.getInstance().setProperty(ID_PER_BACK,perBak.ToString());

            this.Close();
        }

        private void chkNoWp_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNoWp.Checked)
            {
                btnImg.Enabled = false;
                rBCent.Enabled = false;
                rBExp.Enabled = false;
                rBMos.Enabled = false;
                pictureBox.Image = null;
                pictureBox.Refresh();
            }
            else
            {
                btnImg.Enabled = true;
                rBCent.Enabled = true;
                rBExp.Enabled = true;
                rBMos.Enabled = true;
                if(!"".Equals(wallpapper))
                    pictureBox.Image = Image.FromFile(wallpapper);
                pictureBox.Refresh();
            }
        }
    }
}
